<template>
    <div class="board-column">
        <div class="board-column-header">
            <template v-if="unused">
                未使用大类列表
            </template>
            <template v-else>
                <template v-if="isEdit">
                    <el-input
                        ref="input"
                        v-model="headerText"
                        maxlength="8"
                        show-word-limit
                        @blur="handleBlur"
                    />
                </template>
                <template v-else>
                    <el-col :span="20">
                        {{ headerText }}
                    </el-col>
                    <el-col :span="4">
                        <i class="el-icon-edit" :style="{cursor: 'pointer', visibility: visible} " @click="handleEdit" />
                    </el-col>
                </template>
            </template>
        </div>
        <draggable
            :list="list"
            v-bind="$attrs"
            class="board-column-content"
            :set-data="setData"
        >
            <div v-for="element in list" :key="element.id" class="board-item">
                {{ element.name }} ID:{{ element.id }}
            </div>
        </draggable>
    </div>
</template>

<script>
import draggable from 'vuedraggable'

export default {
    name: 'DragKanbanDemo',
    components: {
        draggable
    },
    props: {
        unused: {
            type: Boolean,
            default: false
        },
        value: {
            type: String,
            default: 'Header'
        },
        options: {
            type: Object,
            default() {
                return {}
            }
        },
        list: {
            type: Array,
            default() {
                return []
            }
        }
    },
    data() {
        return {
            isEdit: false
        }
    },
    computed: {
        headerText: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            }
        },
        visible() {
            return this.list.length === 1 ? 'hidden' : 'visible'
        }

    },
    watch: {
        'list.length': function(val) {
            if (val === 1) {
                this.headerText = this.list[0].name
            }
        }
    },
    methods: {
        setData(dataTransfer) {
            // to avoid Firefox bug
            // Detail see : https://github.com/RubaXa/Sortable/issues/1012
            dataTransfer.setData('Text', '')
        },
        handleEdit() {
            this.isEdit = true
            this.$nextTick(() => {
                this.$refs['input'].focus()
            })
        },
        handleBlur() {
            this.isEdit = false
            // this.$emit('input', this.headerText)
        }
    }
}
</script>
<style lang="scss" scoped>
.board-column {
  min-width: 120px;
    width: 100%;
  min-height: 100px;
  height: auto;
  overflow: hidden;
  background: rgba(238, 244, 250, 0.95);
  border-radius: 3px;

  .board-column-header {
    height: 50px;
    line-height: 50px;
    overflow: hidden;
    padding: 0 20px;
    text-align: center;
    background: #333;
    color: #fff;
    border-radius: 3px 3px 0 0;
  }

  .board-column-content {
    height: auto;
    overflow: hidden;
    border: 10px solid transparent;
    min-height: 60px;
    display: flex;
    justify-content: flex-start;
    flex-direction: column;
    align-items: center;

    .board-item {
      cursor: pointer;
      width: 100%;
      height: 64px;
      margin: 5px 0;
      background-color: #fff;
      text-align: left;
      line-height: 54px;
      padding: 5px 10px;
      box-sizing: border-box;
      border-radius: 4px;
      border: 1px solid #ebeef5;
      box-shadow: 0px 0.2px 0.2px 0 rgba(0, 0, 0, 0.2);
    }
  }
}
</style>

